<?php

// $Id$

require_once(TANGRA_MAIN_DIR.'db/paginate_db_data.class.php');
require_once(TANGRA_MAIN_DIR.'grids/grid_ctrl.class.php');

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/grids/_ci/ci_nom_select/ci_nom_select_grid.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/nomenclature/tcms_nomenclature_value_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/ci_attr_types/tcms_cia_nomenclature_dbc.class.php');


class CI_NOM_Select_Grid_Ctrl extends Grid_Ctrl {
	private $nom_id;
	private $ci_id;
	private $ciat_id;

	function __construct($system_name, Vars_Manager $vm, $method = Tangra_Parameter_Method::GET, $nom_id, $ci_id, $ciat_id) {
		parent::__construct($system_name, $vm, $method);
		$this->nom_id = $nom_id;
		$this->ci_id = $ci_id;
		$this->ciat_id = $ciat_id;

//		$this->set_user_defined_post_processing_method('postprocessing');
	}


	protected function create_grid() {
		$ret = new CI_Nom_Select_Grid();

		return $ret;
	}


	protected function create_paginate_db_data() {
		$sqls = '';
		$sqlc = '';

		$pdbd = new Paginate_DB_Data($this->grid->get_page_size(), $sqls, $sqlc);

		return $pdbd;
	}


	public function process(DB_Connection $dbc, $page = NULL) {
		$vm = $this->get_vm();

		$sql = $this->get_sqls($vm);

		$this->paginate_db_data->set_sql_select($sql['sqls']);
		$this->paginate_db_data->set_sql_count($sql['sqlc']);

		parent::process($dbc, $page);
	}


	private function get_sqls($vm) {
		$text = $vm->get_page_var('ci_nom_select', 'text');
		$published = $vm->get_page_var('ci_nom_select', 'show');

		$sqls = TCMS_Nomenclature_Value_DBC::get_sql_for_grid_nom($this->nom_id, $this->ci_id, $this->ciat_id, $text, $published);
		$sqlc = TCMS_Nomenclature_Value_DBC::get_sql_count_for_grid_nom($this->nom_id, $this->ci_id, $this->ciat_id, $text, $published);

		$sql['sqls'] = $sqls;
		$sql['sqlc'] = $sqlc;

		return $sql;
	}


//	protected function postprocessing(DB_Connection $dbc, $rows) {
//		foreach($rows as $key => &$row) {
//			$row[2] = TCMS_CIA_Nomenclature_DBC::exists_with_same_ci_ciat_value($dbc, $this->ci_id, $this->ciat_id, $row[0]);
//		}
//
//		return $rows;
//	}
//
}